<?php

$id = empty($this->Param['id']) ? 0 : $this->Param['id'];

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('SettingHelper');
$SettingHelper = new SettingHelper();

if (empty($this->Param['option'])) {
    $dp_id = $this->Param['dp_id'];
    $data = $MemberHelper->getMember($id, true);
    if($data){
        $u_dpbank_array = explode(',',$data['u_dpbank']);
        $u_bank_array = explode(',',$data['u_bank']);
        $new_bank_array = array_intersect($u_dpbank_array,$u_bank_array);
        $data['u_bank'] = implode(',',$new_bank_array);
        $MemberHelper -> memberUpdate($data,$id);
        $data['u_dp_regroup'] = $data['u_dp_type'] . '_' . $data['u_dp_id'];
        $data['u_addtime'] = empty($data['u_addtime']) ? date('Y-m-d H:i:s', NOW_TIME) : date('Y-m-d H:i:s', $data['u_addtime']);
    }
    $this->Assign('data', $data);
    $this->Assign('id', $id);
    $this->Assign('dp_id', $dp_id);
    
    $audit = $SettingHelper->auditAll(array('`au_status` = ?' => 'ok'));
    $this->Assign('audit', $audit);
    //var_dump($data);
    echo $this->GetView('member_edit.php');
} else {
    switch ($this->Param['option']) {
        case 'save':
            $this->Param['u_addtime'] = strtotime($this->Param['u_addtime']);
            if($id > 0){
                $result = $MemberHelper->memberSave($this->Param, $id);
            }else{
                $result = $MemberHelper->memberSave($this->Param);
            }
            if ($result['success']) {
                if($id == 0){
                     $this->LoadResurces('sms/class.sms');
                     $sms = new Sms();
                     $sms_send = $sms->sendsms($this->Param['u_mobile'], '您的账号：'.$this->Param['u_name'].' 登录密码:'.$result['data']['pwd'].' 登录网址：http://crm.app.qiche100.com');
                }
                $desc = $id > 0 ? '修改用户信息' : '新增用户信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller . '_' . $this->Action);
                $msg = $id>0 ? '保存成功' :'保存成功,密码为:'.$result['data']['pwd'];
                ErrorMsg::Debug($msg,TURE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'remove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $MemberHelper->memberRemove($id);
            if ($result['success']) {
                $MemberHelper->saveLog($this->UserConfig, '删除用户', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug('删除成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'recover':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $MemberHelper->memberRecover($id);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '恢复用户', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug('恢复成功', TRUE);
            }else{
                ErrorMsg::Debug('恢复失败');
            }
            break;
        default :
    }
}